/*
 * DXSprite.cpp
 *
 *  Created on: Feb 5, 2010
 *      Author: dcrown
 */

#include "Sprite.h"
#include "..\Systems\Logger.h"
#include "DXManager.h"

extern DXManager* gpDX;

void Sprite::Clear()
{
	scale = D3DXVECTOR2(1.0f, 1.0f);
	translate = D3DXVECTOR2(0.0f, 0.0f);
	rotCenter = D3DXVECTOR2(0.0f, 0.0f);
	rotate = 0.0f;
	texture = NULL;
	r = NULL;
	id = "";
}

void Sprite::Destroy()
{
	delete texture;
	texture = NULL;
}

bool Sprite::Create(string id, Texture* texture)
{
	if (gpDX->LoadSprite(&sprite))
	{
		this->id = id;
		this->texture = texture;
		return true;
	}
	else
	{
		Logger::getInstance().LOG(Logger::FATAL, "Could not create sprite");
		return false;
	}
}

void Sprite::ScaleOriginal(float x, float y)
{
	scale.x = x;
	scale.y = y;
}

void Sprite::Scale(float x, float y)
{
	scale.x *= x;
	scale.y *= y;
}

void Sprite::TranslateOriginal(int x, int y)
{
	translate.x = x;
	translate.y = y;
}

void Sprite::Translate(int x, int y)
{
	translate.x += x;
	translate.y += y;
}

void Sprite::ResetTransform()
{
	scale = D3DXVECTOR2(1.0f, 1.0f);
	translate = D3DXVECTOR2(0.0f, 0.0f);
	rotCenter = D3DXVECTOR2(0.0f, 0.0f);
	rotate = 0.0f;
}

void Sprite::RotateOriginal(float rads, int x, int y)
{
	rotate = rads;
	rotCenter = D3DXVECTOR2(x, y);
}

void Sprite::Rotate(float rads)
{
	rotate += rads;
}

//called by game loop
void Sprite::Update()
{
	//scale = D3DXVECTOR2(1.0f, 1.0f);
	//Logger::getInstance().LOG(Logger::DEBUG, "scale.x=%f, scale.y=%f", scale.x, scale.y);
	gpDX->ApplyTransform(&sprite, NULL, 0.0, &scale, &rotCenter, rotate, &translate);
}

//called by game loop
void Sprite::Draw()
{
	gpDX->Draw(sprite, texture->GetTexture(), r);
}


void Sprite::SetTextureRect(RECT* r)
{
	this->r = r;
}

void Sprite::SetTexture(Texture* texture)
{
	this->texture = texture;
	ResetTransform();
}
